package com.health_predict_springboot.service.impl;

import com.health_predict_springboot.dao.SysUserMapper;
import com.health_predict_springboot.pojo.SysUser;
import com.health_predict_springboot.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Override
    public SysUser getUserById(Long userId) {
        return sysUserMapper.selectByUserId(userId);
    }

    @Override
    public SysUser getUserByPhone(String phone) {
        return sysUserMapper.selectByPhone(phone);
    }

    @Override
    public List<SysUser> getAllUsers() {
        return sysUserMapper.selectAllUsers();
    }

    @Override
    public int addUser(SysUser user) {
        // 设置创建时间和更新时间
        Date now = new Date();
        user.setCreateTime(now);
        user.setUpdateTime(now);
        // 默认密码为123456（MD5加密）
        if (user.getPassword() == null || user.getPassword().isEmpty()) {
            user.setPassword("e10adc3949ba59abbe56e057f20f883e");
        }
        // 默认状态为待审核
        if (user.getStatus() == null) {
            user.setStatus(0);
        }
        return sysUserMapper.insertUser(user);
    }

    @Override
    public int updateUser(SysUser user) {
        // 设置更新时间
        user.setUpdateTime(new Date());
        return sysUserMapper.updateUser(user);
    }

    @Override
    public int deleteUser(Long userId) {
        return sysUserMapper.deleteUser(userId);
    }

    @Override
    public List<SysUser> getUsersByType(Integer userType) {
        return sysUserMapper.selectUsersByType(userType);
    }

    @Override
    public List<SysUser> getUsersByPage(Map<String, Object> params) {
        return sysUserMapper.selectUsersByPage(params);
    }

    @Override
    public int getUserCount(Map<String, Object> params) {
        return sysUserMapper.selectUserCount(params);
    }

    @Override
    public SysUser login(String phone, String password) {
        // 查询用户
        SysUser user = sysUserMapper.selectByPhone(phone);
        if (user != null) {
            // 验证密码（这里简单比较，实际应该使用密码加密验证）
            if (user.getPassword().equals(password)) {
                // 检查用户状态
                if (user.getStatus() == 1) { // 正常状态
                    return user;
                }
            }
        }
        return null;
    }

    @Override
    public int changePassword(Long userId, String oldPassword, String newPassword) {
        // 查询用户
        SysUser user = sysUserMapper.selectByUserId(userId);
        if (user != null) {
            // 验证旧密码
            if (user.getPassword().equals(oldPassword)) {
                // 更新密码
                user.setPassword(newPassword);
                user.setUpdateTime(new Date());
                return sysUserMapper.updateUser(user);
            }
        }
        return 0;
    }
}